package com.anta.crm.codegen.generator.element;

import com.anta.crm.codegen.utils.CustomerFormattingUtil;
import org.mybatis.generator.api.IntrospectedColumn;
import org.mybatis.generator.api.dom.OutputUtilities;
import org.mybatis.generator.api.dom.java.FullyQualifiedJavaType;
import org.mybatis.generator.api.dom.xml.Attribute;
import org.mybatis.generator.api.dom.xml.TextElement;
import org.mybatis.generator.api.dom.xml.XmlElement;
import org.mybatis.generator.codegen.mybatis3.ListUtilities;
import org.mybatis.generator.codegen.mybatis3.MyBatis3FormattingUtilities;
import org.mybatis.generator.codegen.mybatis3.xmlmapper.elements.AbstractXmlElementGenerator;
import org.mybatis.generator.config.GeneratedKey;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import static org.mybatis.generator.internal.util.StringUtility.stringHasValue;

public class CustomerInsertElementGenerator extends AbstractXmlElementGenerator {

    private final boolean isSimple;

    public CustomerInsertElementGenerator(boolean isSimple) {
        this.isSimple = isSimple;
    }

    public void addElements(XmlElement parentElement) {
        FullyQualifiedJavaType parameterType;
        if (this.isSimple) {
            parameterType = new FullyQualifiedJavaType(this.introspectedTable.getBaseRecordType());
        } else {
            parameterType = this.introspectedTable.getRules().calculateAllFieldsClass();
        }

        XmlElement answer = this.buildInitialInsert(this.introspectedTable.getInsertStatementId(), parameterType);
        StringBuilder insertClause = new StringBuilder();
        insertClause.append("insert into "); //$NON-NLS-1$
        insertClause.append(introspectedTable
                .getFullyQualifiedTableNameAtRuntime());
        insertClause.append(" ("); //$NON-NLS-1$
        answer.addElement(new TextElement(insertClause.toString()));
        insertClause.setLength(0);
        OutputUtilities.xmlIndent(insertClause, 1);

        StringBuilder valuesClause = new StringBuilder();
        valuesClause.append("values ("); //$NON-NLS-1$

        List<String> valuesClauses = new ArrayList<>();
        List<IntrospectedColumn> columns =
                ListUtilities.removeIdentityAndGeneratedAlwaysColumns(introspectedTable.getAllColumns());
        for (int i = 0; i < columns.size(); i++) {
            IntrospectedColumn introspectedColumn = columns.get(i);

            insertClause.append(MyBatis3FormattingUtilities
                    .getEscapedColumnName(introspectedColumn));
            valuesClause.append(getParameterClause(introspectedColumn));
            if (i + 1 < columns.size()) {
                insertClause.append(","); //$NON-NLS-1$
                valuesClause.append(","); //$NON-NLS-1$
            }

            answer.addElement(new TextElement(insertClause.toString()));
            insertClause.setLength(0);
            OutputUtilities.xmlIndent(insertClause, 1);

            valuesClauses.add(valuesClause.toString());
            valuesClause.setLength(0);
            OutputUtilities.xmlIndent(valuesClause, 1);
        }

        insertClause.append(')');
        answer.addElement(new TextElement(insertClause.toString()));
        valuesClause.append(')');
        valuesClauses.add(valuesClause.toString());
        Iterator var10 = valuesClauses.iterator();

        while(var10.hasNext()) {
            String clause = (String)var10.next();
            answer.addElement(new TextElement(clause));
        }

        if (this.context.getPlugins().sqlMapInsertElementGenerated(answer, this.introspectedTable)) {
            parentElement.addElement(answer);
        }

    }

//    private boolean isSimple;
//
//    public CustomerInsertElementGenerator(boolean isSimple) {
//        super();
//        this.isSimple = isSimple;
//    }
//
//    @Override
//    public void addElements(XmlElement parentElement) {
//        XmlElement answer = new XmlElement("insert"); //$NON-NLS-1$
//
//        answer.addAttribute(new Attribute(
//                "id", introspectedTable.getInsertStatementId())); //$NON-NLS-1$
//
//        FullyQualifiedJavaType parameterType;
//        if (isSimple) {
//            parameterType = new FullyQualifiedJavaType(
//                    introspectedTable.getBaseRecordType());
//        } else {
//            parameterType = introspectedTable.getRules()
//                    .calculateAllFieldsClass();
//        }
//
//        answer.addAttribute(new Attribute("parameterType", //$NON-NLS-1$
//                parameterType.getFullyQualifiedName()));
//
//        context.getCommentGenerator().addComment(answer);
//
//        GeneratedKey gk = introspectedTable.getGeneratedKey();
//        if (gk != null) {
//            introspectedTable.getColumn(gk.getColumn()).ifPresent(introspectedColumn -> {
//                // if the column is null, then it's a configuration error. The
//                // warning has already been reported
//                if (gk.isJdbcStandard()) {
//                    answer.addAttribute(new Attribute(
//                            "useGeneratedKeys", "true")); //$NON-NLS-1$ //$NON-NLS-2$
//                    answer.addAttribute(new Attribute(
//                            "keyProperty", introspectedColumn.getJavaProperty())); //$NON-NLS-1$
//                    answer.addAttribute(new Attribute(
//                            "keyColumn", introspectedColumn.getActualColumnName())); //$NON-NLS-1$
//                } else {
//                    answer.addElement(getSelectKey(introspectedColumn, gk));
//                }
//            });
//        }
//
//        StringBuilder insertClause = new StringBuilder();
//
//        insertClause.append("insert into "); //$NON-NLS-1$
//        insertClause.append(introspectedTable
//                .getFullyQualifiedTableNameAtRuntime());
//        insertClause.append(" ("); //$NON-NLS-1$
//        answer.addElement(new TextElement(insertClause.toString()));
//        insertClause.setLength(0);
//        OutputUtilities.xmlIndent(insertClause, 1);
//
//        StringBuilder valuesClause = new StringBuilder();
//        valuesClause.append("values ("); //$NON-NLS-1$
//
//        List<String> valuesClauses = new ArrayList<>();
//        List<IntrospectedColumn> columns =
//                ListUtilities.removeIdentityAndGeneratedAlwaysColumns(introspectedTable.getAllColumns());
//        for (int i = 0; i < columns.size(); i++) {
//            IntrospectedColumn introspectedColumn = columns.get(i);
//
//            insertClause.append(MyBatis3FormattingUtilities
//                    .getEscapedColumnName(introspectedColumn));
//            valuesClause.append(getParameterClause(introspectedColumn));
//            if (i + 1 < columns.size()) {
//                insertClause.append(","); //$NON-NLS-1$
//                valuesClause.append(","); //$NON-NLS-1$
//            }
//
//            answer.addElement(new TextElement(insertClause.toString()));
//            insertClause.setLength(0);
//            OutputUtilities.xmlIndent(insertClause, 1);
//
//            valuesClauses.add(valuesClause.toString());
//            valuesClause.setLength(0);
//            OutputUtilities.xmlIndent(valuesClause, 1);
//        }
//
//        insertClause.append(')');
//        answer.addElement(new TextElement(insertClause.toString()));
//
//        valuesClause.append(')');
//        valuesClauses.add(valuesClause.toString());
//
//        for (String clause : valuesClauses) {
//            answer.addElement(new TextElement(clause));
//        }
//
//        if (context.getPlugins().sqlMapInsertElementGenerated(answer,
//                introspectedTable)) {
//            parentElement.addElement(answer);
//        }
//    }
//
    private String getParameterClause(IntrospectedColumn introspectedColumn) {
        String javaProperty = introspectedColumn.getJavaProperty();
        if("createTime".equals(javaProperty)
            || "updateTime".equals(javaProperty)) {
            return "SYSDATE()";
        }
        return CustomerFormattingUtil.getParameterClause(introspectedColumn);
    }
}
